package controllers

import (
	"errors"
	"github.com/gogf/gf/os/glog"
	"mdblog/models/auth"
)

type BlogApiController struct {
	AjaxController
}

func (o *BlogApiController) Login() {
	request := UserLoginRequest{}
	if err := o.LoadRequest(&request); err != nil {
		o.ErrorResponse(err)
	} else {
		err := request.Do(o)
		if err != nil {
			text := ""
			switch err {
			case auth.PasswordError:
				text = "密码错误"
			case auth.UserDoesNotExist:
				text = "用户不存在"
			case auth.UsernameAndEmailEmptyError:
				text = "用户名或者邮箱不能同时为空"
			case CaptchaError:
				text = CaptchaError.Error()
			default:
				glog.Errorf("%s", err.Error())
				text = "未知错误请联系管理员查证"
			}
			o.ErrorResponse(errors.New(text))
		} else {
			o.MessageResponse("ok")
		}
	}
}

func (o *BlogApiController) Logout() {
	o.DestroySession()
	o.MessageResponse("ok")
}
